package com.taplinker.client.core;

import java.io.File;

import android.text.TextUtils;

import com.taplinker.client.AppConfigContext;
import com.taplinker.client.widget.image.IImageLoader;
import com.taplinker.core.rpc.Result;
import com.taplinker.core.rpc.command.SimpleBaseCommand;
import com.taplinker.core.rpc.http.HttpTemplate;

public class UploadFileManager {
	private static UploadFileManager instance;
	
	private UploadFileManager() {
	}
	
	public static UploadFileManager getInstance() {
		if(instance != null) {
			return instance;
		}
		synchronized (UploadFileManager.class) {
			if (instance == null) {
				instance = new UploadFileManager();
			}
		}
		return instance;
	}
	
	/**
	 * 下载文件
	 * @param url
	 * @param ivLoader 显示进度
	 * @param callBack
	 */
	public void loadFile(String url, final IImageLoader ivLoader, final CallBack callBack) {
		if (TextUtils.isEmpty(url)) return;
		// 网络文件
		if (url.contains("/files/")) {
			url = AppConfigContext.WEB_DISK_ROOT + url;
			new DownloadFileCommand(url) {
				
				@Override
				public void yes(Result result) {
					if(callBack != null ) {
						callBack.success((String) result.object);
					}
				}

				@Override
				public void no(Result result) {
					if(callBack != null ) {
						callBack.failure("下载失败，" + result.message);
					}
				}

				@Override
				protected void update(long planSize, long curSize) {
					if(ivLoader != null) {
						ivLoader.updateProgress(planSize, curSize);
					}
				}
				
			}.execute();
		// 本地文件
		} else {
			if(callBack != null ) {
				callBack.success(url);
			}
		}
        
	}
	
	/**
	 * 上传文件
	 * @param url
	 * @param ivLoader 显示进度
	 * @param callBack
	 */
	public void uploadFile(final File file, final IImageLoader ivLoader, final CallBack callBack) {
		if (file == null || !file.exists()) return;
		new UploadFileCmd(file) {
			
			@Override
			public void yes(Result result) {
				if(callBack != null ) {
					callBack.success((String) result.object);
				}
			}

			@Override
			public void no(Result result) {
				if(callBack != null ) {
					callBack.failure("下载失败，" + result.message);
				}
			}

			@Override
			protected void update(long planSize, long curSize) {
				if(ivLoader != null) {
					ivLoader.updateProgress(planSize, curSize);
				}
			}
			
		}.execute();
		
	}
	
   	class DownloadFileCommand extends SimpleBaseCommand{
   		private String url;
   		
   		public DownloadFileCommand(String url) {
   			this.url = url;
   		}
   		
   		@Override
   		public Result go() {
   			Result result = new Result();
   			try {
   				String requestUrl = url;
   				if(url.contains("http://")) {
   					requestUrl = new HttpTemplate().download(url, this).object.toString();	
   				}
   				result.object = requestUrl;
   				return result;
   			} catch (Exception e) {
   				e.printStackTrace();
   			}
   			return result;
   		}
   	}
	
	class UploadFileCmd extends SimpleBaseCommand {
		private File file;
		
		public UploadFileCmd(File file) {
			super();
			this.file = file;
		}
		
		public UploadFileCmd(String path) {
			super();
			this.file = new File(path);
		}
		
		@Override
		public Result go() {
			return new HttpTemplate().upload(AppConfigContext.TAPLINKER_WEB + "/services/v2/upload" , file, this);
		}

	}
	
	public interface CallBack {
		public void success(String path);
		public void failure(String message);
	}
}
